﻿using CIS.Web.Common;
using CIS.Web.Models;
using System.Data.Entity;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace CIS.Web.Controllers
{
	[AllowAnonymous]
	public class MeetingNewsInfoController : BaseDbController
	{
		public async Task<ActionResult> Index(string id)
		{
			var newsInfo = await DbContext.NewsInfo
					.FirstOrDefaultAsync(x => x.ID == id && x.Status == ConstValues.Status.正常);

			if (newsInfo == null)
			{
				return RedirectToAction(nameof(HomeController.MainIndex), nameof(HomeController).GetControllerName());
			}

			var newListInfo = await DbContext.NewsListInfo.AsNoTracking()
					.FirstOrDefaultAsync(x => x.ID == newsInfo.NewsListId);

			var conferenceMaster = await DbContext.ConferenceMaster.AsNoTracking()
					.FirstOrDefaultAsync(x => x.ID == newsInfo.ConferenceId);

			var model = new MeetingNewsInfoModel();
			model.Id = id;
			model.ConferenceId = newsInfo.ConferenceId;
			model.MainText = newsInfo.MainText == null ? string.Empty : newsInfo.MainText;
			model.CreateDate = newsInfo.CreateDate.Value.ToString("yyyy-MM-dd HH:ss");
			model.Creator = newsInfo.Creator;
			model.NewsTitle = newsInfo.NewsTitle;
			model.CollectionCount = newsInfo.CollectionCount;
			model.NewsListName = newListInfo.NewsListName;
			model.ConferenceNickName = conferenceMaster.ConferenceNickName;
			model.NewsListId = newListInfo.ID;
			model.Newslink = newsInfo.Link;
			model.Newspic = newsInfo.MainPic;
			model.Abstract = newsInfo.Abstract == null ? string.Empty : newsInfo.Abstract;
			model.ConferenceName = conferenceMaster.ConferenceName;
			model.IsInternational = conferenceMaster.IsInternational;
			model.Keywords = conferenceMaster.Keywords;
			model.Description = conferenceMaster.Description;
			model.MainTextShared = newsInfo.MainText == null ? string.Empty : newsInfo.MainText.Replace("\r", "").Replace("\n", "").Replace("<p>", "").Replace("</p>", "").Replace("&nbsp;", "");
			model.IsShowCountdown = conferenceMaster.IsShowCountdown;
			model.PicForBackgroundPath = string.IsNullOrEmpty(conferenceMaster.PicForBackground) ? string.Empty :
						conferenceMaster.PicForBackground.Replace("~", "").Replace("\\", "/");
			model.OpacityOfPic = conferenceMaster.OpacityOfPic;
			model.IsFollowBackground = conferenceMaster.IsFollowBackground;
			model.ColorForBackground = conferenceMaster.ColorForBackground;
			model.OpacityOfColor = conferenceMaster.OpacityOfColor;
			model.ColorForMenu = conferenceMaster.ColorForMenu;
			model.OpacityOfMenu = conferenceMaster.OpacityOfMenu;
			model.ColorForLeft = conferenceMaster.ColorForLeft;
			model.OpacityOfLeft = conferenceMaster.OpacityOfLeft;
			model.ColorForCenter = conferenceMaster.ColorForCenter;
			model.OpacityOfCenter = conferenceMaster.OpacityOfCenter;
			model.ColorForRight = conferenceMaster.ColorForRight;
			model.OpacityOfRight = conferenceMaster.OpacityOfRight;
			model.ColorForConference = conferenceMaster.ColorForConference;
			model.FontColorForMenu = conferenceMaster.FontColorForMenu;
			model.IsShowShare = conferenceMaster.IsShowShare;
			model.NewsBackgroundColor = conferenceMaster.NewsBackgroundColor;
			model.NewsBackgroundOpacity = conferenceMaster.NewsBackgroundOpacity;

			newsInfo.ViewCount += 1;
			await DbContext.SaveChangesAsync();

			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName, "Index", model.ConferenceId, model.NewsListId, model.Id);

			return View(model);
		}
	}
}